﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using SpLocalizer.Common;
using Microsoft.SharePoint;
using Microsoft.Practices.Unity;

namespace SpLocalizer.ContentSourceProviders.SpListItems
{
    internal class SpListItemFieldUrlValueLocalizationTask : SpListItemAbstractValueLocalizationTask
    {
        protected override string GetFieldTextToTranslate(SPListItem item)
        {
            try
            {
                SPFieldUrlValue urlValue = new SPFieldUrlValue(item[this.FieldId].ToString());
                return urlValue.Description;
            }
            catch
            {
                return null;
            }
               
        }

        internal override void SetFieldTextValue(SPListItem item)
        {
            SPFieldUrlValue urlValue = new SPFieldUrlValue(item[this.FieldId].ToString());
                
                urlValue.Description = this.TranslatedText;
                if (!string.IsNullOrEmpty(urlValue.Url))
                    urlValue.Url = urlValue.Url.Replace(this.FromLanguage.Name, this.ToLanguage.Name);

                item[this.FieldId] = urlValue.ToString();
        }
    }
}
